using System;
using System.Collections.Generic;
using System.Text;
using TaugeLibrary.Classes.Attributes;
using TaugeFramework.Forms;
using System.Windows.Forms;
using TaugeLibrary.Enums;
using System.Reflection;
using TaugeLibrary.Classes;

namespace TaugeFramework.Classes
{
    class UIManager
    {
        public static Form createUI(Object o)
        {
            /*
            AttBindUI _ui = (AttBindUI)TaugeLibrary.Classes.LibConstants.getAttribute(o.GetType(),typeof(AttBindUI));
            if (_ui == null)
                return null;

            frmBusinessUnit fb = new frmBusinessUnit();
            UserControl x = (UserControl)Activator.CreateInstance(Type.GetType(_ui.UI), null);
            fb.setPnlMainSize(x.Width, x.Height);
            fb.pnlUI.Controls.Add(x);
            x.Dock = DockStyle.Fill;            
            return fb;
             * */
            return null;
        }


        public static void writeUI(Object o, ContainerControl f)
        {            
            System.Reflection.PropertyInfo[] _obj_prop = o.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in _obj_prop)
            {
                AttBindControl _bind = (AttBindControl)TaugeLibrary.Classes.LibConstants.getAttribute(pi, typeof(AttBindControl));
                if (_bind != null)
                {
                    switch (_bind.ControlType)
                    {
                        case eControlType.LABEL:
                        case eControlType.TEXTBOX:
                            Control _txtbox = null;
                            if(_bind.BindParentForm)
                                 _txtbox = LibConstants.getControl(f.ParentForm, _bind.ControlName);                            
                            else
                                _txtbox = LibConstants.getControl(f, _bind.ControlName);                            
                            if (_txtbox != null)
                            {
                                PropertyInfo _ctlprop = _txtbox.GetType().GetProperty("Text");
                                _ctlprop.SetValue(_txtbox, pi.GetValue(o,null).ToString(), null);
                            }
                            break;
                        case eControlType.COMBOBOX:
                            ComboBox _cbobox = null;
                            if(_bind.BindParentForm)
                                _cbobox = (ComboBox)LibConstants.getControl(f.ParentForm, _bind.ControlName);
                            else
                                _cbobox = (ComboBox)LibConstants.getControl(f, _bind.ControlName);

                            if (_cbobox != null)
                            {
                                foreach (TaugeListItem x in _cbobox.Items)
                                {
                                    if (x.Value.ToString() == pi.GetValue(o, null).ToString())
                                    {
                                        _cbobox.Text  = x.Text;
                                        break;
                                    }
                                }
                                
                            }
                            break;
                        case eControlType.CHECKBOX:
                            Control _ctl = null;
                            if(_bind.BindParentForm)
                                _ctl =  LibConstants.getControl(f.ParentForm, _bind.ControlName);
                            else
                                _ctl = LibConstants.getControl(f, _bind.ControlName);

                            if (_ctl != null)
                            {
                                PropertyInfo _ctlprop = null;
                                if (_bind.ControlType == eControlType.COMBOBOX)
                                    _ctlprop = _ctl.GetType().GetProperty("SelectedValue");
                                else if (_bind.ControlType == eControlType.CHECKBOX)
                                    _ctlprop = _ctl.GetType().GetProperty("Checked");

                                if(_ctlprop!=null)                                    
                                    _ctlprop.SetValue(_ctl, pi.GetValue(o,null), null);
                            }
                            break;
                        case eControlType.PROPERTY:
                            PropertyInfo _fprop = null;
                            if(_bind.BindParentForm)
                                _fprop = f.ParentForm.GetType().GetProperty(_bind.ControlName);
                            else
                                _fprop = f.GetType().GetProperty(_bind.ControlName);
                            if (_fprop != null)
                                _fprop.SetValue(f, pi.GetValue(o, null), null);
                            break;
                    }
                }
            }
        }

        public static string GetSQLSearchUI(object o, ContainerControl f)
        {
            string result="";
            AttTable _tbl = (AttTable)LibConstants.getAttribute(o.GetType(), typeof(AttTable));
            if (_tbl != null)
            {
                foreach (PropertyInfo pi in o.GetType().GetProperties())
                {
                    AttSearchable _searchable = (AttSearchable)LibConstants.getAttribute(pi, typeof(AttSearchable));
                    if (_searchable != null)
                    {
                        AttBindControl _bind = (AttBindControl)LibConstants.getAttribute(pi, typeof(AttBindControl));
                        if (_bind != null)
                        {
                            AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                            if (_fld != null)
                            {
                                switch (_bind.ControlType)
                                {
                                    case eControlType.TEXTBOX:
                                    case eControlType.LABEL:
                                    case eControlType.CHECKBOX:
                                    case eControlType.COMBOBOX:
                                    case eControlType.OPTBOX:
                                    case eControlType.LISTBOX:
                                        Control ctl = null;
                                        if (_bind.BindParentForm)
                                            ctl  = LibConstants.getControl(f.ParentForm, _bind.ControlName);
                                        else
                                            ctl = LibConstants.getControl(f, _bind.ControlName);

                                        if (ctl != null)
                                        {
                                            if (ctl is TextBox)
                                            {
                                                TextBox txtbox = (TextBox)ctl;
                                                if (txtbox.Text.Trim() != "")
                                                {
                                                    result += " AND " + _fld.FieldName + " LIKE " + LibConstants.sqlStr("%" + txtbox.Text.Trim() + "%");
                                                }
                                            }
                                            else if (ctl is Label)
                                            {
                                                Label lbl = (Label)ctl;
                                                if (lbl.Text.Trim() != "")
                                                {
                                                    result += " AND " + _fld.FieldName + " LIKE " + LibConstants.sqlStr("%" + lbl.Text.Trim() + "%");
                                                }

                                            }
                                        }

                                        break;


                                        
                                    case eControlType.PROPERTY:
                                    
                                        break;
                                }
                            }
                        }
                    }
                }
            }
            return "";
        }

        public static void readUI(Object o, ContainerControl f)
        {
            System.Reflection.PropertyInfo[] _obj_prop = o.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in _obj_prop)
            {
                if (pi.CanWrite)
                {
                    AttBindControl _bind = (AttBindControl)TaugeLibrary.Classes.LibConstants.getAttribute(pi, typeof(AttBindControl));
                    if (_bind != null && !_bind.WriteOnly)
                    {
                        switch (_bind.ControlType)
                        {
                            case eControlType.COMBOBOX:
                                ComboBox _cbo = null;
                                if(_bind.BindParentForm)
                                    _cbo = (ComboBox)LibConstants.getControl(f.ParentForm, _bind.ControlName);
                                else
                                    _cbo = (ComboBox)LibConstants.getControl(f, _bind.ControlName);

                                if (_cbo != null)
                                    pi.SetValue(o, ((TaugeLibrary.Classes.TaugeListItem)_cbo.SelectedItem).Value, null);
                                break;
                            case eControlType.LABEL:
                            case eControlType.TEXTBOX:                            
                            case eControlType.CHECKBOX:
                                Control _ctl = null;
                                if(_bind.BindParentForm)
                                    _ctl = LibConstants.getControl(f.ParentForm, _bind.ControlName);
                                else
                                    _ctl = LibConstants.getControl(f, _bind.ControlName);
                                if (_ctl != null)
                                {
                                    /*
                                    else if (_bind.ControlType == eControlType.COMBOBOX)
                                        _ctlprop = _ctl.GetType().GetProperty("SelectedValue");
                                    */
                                    PropertyInfo _ctlprop = null;
                                    if (_bind.ControlType == eControlType.TEXTBOX||_bind.ControlType == eControlType.LABEL)
                                        _ctlprop = _ctl.GetType().GetProperty("Text");
                                    else if (_bind.ControlType == eControlType.CHECKBOX)
                                        _ctlprop = _ctl.GetType().GetProperty("Checked");
                                    if (_ctlprop != null)
                                        pi.SetValue(o, _ctlprop.GetValue(_ctl, null), null);
                                }
                                break;
                            case eControlType.PROPERTY:
                                PropertyInfo _fprop = null;
                                if(_bind.BindParentForm)
                                    _fprop = f.ParentForm.GetType().GetProperty(_bind.ControlName);
                                else
                                    _fprop = f.GetType().GetProperty(_bind.ControlName);
                                if (_fprop != null)
                                    pi.SetValue(o, _fprop.GetValue(f, null), null);
                                break;
                        }
                    }
                }

            }

        }
    }
}
